<%@page import="pl.pr3sto.security.Role" %>

<div id="authentication-bar">
	<div id="admin-links">
	<g:if test="${ session.profile.role == Role.SUPERADMIN }">
		<g:link controller="admin" action="index">SUPERADMIN PANEL</g:link> |
	</g:if>
	<g:if test="${ session.profile.role == Role.ADMIN || session.profile.role == Role.SUPERADMIN }">
		<g:link controller="admin" action="index">ADMIN PANEL</g:link>
	</g:if>
	</div>
	<g:if test="${ session.profile.role == Role.GUEST }">
		<g:if test="${flash.message}">
			<div class="error">
				<p>${ message(code: flash.message) }</p>
			</div>
		</g:if>
		<form action="#" id="loginForm">
			<label for="login"><g:message code="login.name"/> </label>
			<input type="text" name="login" class="login" value=""/>
			<label for="pass"><g:message code="login.password"/></label>
			<input type="password" name="pass" class="password" value=""/>
			<input type="submit" id="submit-btn" value="${ message(code: 'login.login') }" >
		</form>
	</g:if>
	<g:else>
		<div id="greeting-bar">
			<g:message code="authBar.welcome" />${ session.profile.person }
			<a href="#" id="logout"><g:message code="login.logout"/></a>
		</div>
	</g:else>
</div>

<script type="text/javascript">
$(document).ready(function(){

	pl.pr3sto.form.bindSubmitHandler({
		id: 'loginForm',
		url: '${ createLink(controller: 'profile', action: 'loginUser')}',
		success: function(result) {
           	$('#authentication-bar').replaceWith(result);
		},
		error: function(result) {
			console.log(result);
		}
	});

	pl.pr3sto.link.bindAjaxAction({
		id: 'logout',
		url: '${ createLink(controller: 'profile', action: 'logoutUser')}',
		data: '#',
		success: function(result) {
			$('#authentication-bar').replaceWith(result);
		},
		error: function(result) {
			console.log(result);
		}
	});
});

</script>
